package com.ruoyi.gpt.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.annotation.RateLimiter;
import com.ruoyi.common.enums.LimitType;
import com.ruoyi.gpt.queue.ApiKeysQueue;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gpt.domain.vo.ChatTokenVo;
import com.ruoyi.gpt.domain.bo.ChatTokenBo;
import com.ruoyi.gpt.service.IChatTokenService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * chatGpt的token
 *
 * @author ztx
 * @date 2023-03-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/gpt/chatToken")
public class ChatTokenController extends BaseController {

    private final IChatTokenService iChatTokenService;

    /**
     * 查询chatGpt的token列表
     */
    @SaCheckPermission("gpt:chatToken:list")
    @GetMapping("/list")
    // @RateLimiter(count = 2, time = 10, limitType = LimitType.IP) // 限流测试
    public TableDataInfo<ChatTokenVo> list(ChatTokenBo bo, PageQuery pageQuery) {
        return iChatTokenService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出chatGpt的token列表
     */
    @SaCheckPermission("gpt:chatToken:export")
    @Log(title = "chatGpt的token", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ChatTokenBo bo, HttpServletResponse response) {
        List<ChatTokenVo> list = iChatTokenService.queryList(bo);
        ExcelUtil.exportExcel(list, "chatGpt的token", ChatTokenVo.class, response);
    }

    /**
     * 获取chatGpt的token详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("gpt:chatToken:query")
    @GetMapping("/{id}")
    public R<ChatTokenVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iChatTokenService.queryById(id));
    }

    /**
     * 新增chatGpt的token
     */
    @SaCheckPermission("gpt:chatToken:add")
    @Log(title = "chatGpt的token", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ChatTokenBo bo) {
        Boolean aBoolean = iChatTokenService.insertByBo(bo);
        if(aBoolean){
            ApiKeysQueue.init();
        }
        return toAjax(aBoolean);
    }

    /**
     * 修改chatGpt的token
     */
    @SaCheckPermission("gpt:chatToken:edit")
    @Log(title = "chatGpt的token", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ChatTokenBo bo) {
        Boolean aBoolean = iChatTokenService.updateByBo(bo);
        if(aBoolean){
            ApiKeysQueue.init();
        }
        return toAjax(aBoolean);
    }

    /**
     * 删除chatGpt的token
     *
     * @param ids 主键串
     */
    @SaCheckPermission("gpt:chatToken:remove")
    @Log(title = "chatGpt的token", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        Boolean aBoolean = iChatTokenService.deleteWithValidByIds(Arrays.asList(ids), true);
        if(aBoolean){
            ApiKeysQueue.init();
        }
        return toAjax(aBoolean);
    }
}
